﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualBasic;
using System.Web.UI;
using System.Reflection;

namespace Common
{
    /// <summary>
    /// 转换类
    /// </summary>
    public class Converter
    {
        /// <summary>
        /// 繁体转简体
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string Traditional2Simplified(string str)
        { // 
            return (Microsoft.VisualBasic.Strings.StrConv(str, Microsoft.VisualBasic.VbStrConv.SimplifiedChinese, 0));
        }
        /// <summary>
        /// 简体转繁体
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string Simplified2Traditional(string str)
        { // 
            return (Microsoft.VisualBasic.Strings.StrConv(str as String, Microsoft.VisualBasic.VbStrConv.TraditionalChinese, 0));
        }
        /// <summary>
        /// 控件填充到Entity
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <param name="controls"></param>
        public static void ToEntity<T>(ref T t, Control controls)
        {
            ToEntity<T>(ref t, controls, true);
        }
        /// <summary>
        /// 控件填充到Entity
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <param name="controls"></param>
        /// <param name="isComboxValue">是否获取ComBox的Value,true为value，false为text</param>
        public static void ToEntity<T>(ref T t, Control controls, bool isComboxValue)
        {
            Dictionary<string, Control> dict = GetControls(controls);
            foreach (string key in dict.Keys)
            {
                PropertyInfo pInfo = t.GetType().GetProperty(key);
                Control control = (Control)dict[key];
                object value = GetControlValue(control, isComboxValue);
                if (pInfo != null)
                {
                    if (value is String && string.IsNullOrEmpty(value.ToString()))
                    {
                        pInfo.SetValue(t, null, null);
                    }
                    else
                    {
                        try
                        {
                            pInfo.SetValue(t, Convert.ChangeType(value, pInfo.PropertyType), null);
                        }
                        catch { }
                    }

                }
            }

        }
        /// <summary>
        /// Entity填充到控件
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <param name="controls"></param>
        public static void ToControls<T>(T t, Control controls)
        {
            Dictionary<string, Control> dict = GetControls(controls);

            foreach (PropertyInfo pInfo in t.GetType().GetProperties())
            {
                if (dict.ContainsKey(pInfo.Name))
                {
                    object value = pInfo.GetValue(t, null);
                    Control control = (Control)dict[pInfo.Name];
                    SetControlValue(control, value);
                }
            }
        }

        private static Dictionary<string, Control> GetControls(Control controls)
        {
            Dictionary<string, Control> dict = new Dictionary<string, Control>();
            SearchControls(ref dict, controls);
            return dict;
        }
        private static void SearchControls(ref Dictionary<string, Control> dict, Control controls)
        {
            foreach (Control childControl in controls.Controls)
            {
                PropertyInfo propertyInfo = childControl.GetType().GetProperty("BindingContainer");
                if (propertyInfo != null)
                {
                    object value = propertyInfo.GetValue(childControl, null);
                    if (value is Control && value != null && !(string.IsNullOrEmpty(((Control)value).ID)) && ((Control)value).ID.Length > 3 && !dict.ContainsKey(((Control)value).ID.Substring(3)))
                    {
                        dict.Add(((Control)value).ID.Substring(3), (Control)value);
                    }
                }
                if (childControl.Controls.Count > 0)
                {
                    SearchControls(ref dict, childControl);
                }
                else
                {
                    if (!string.IsNullOrEmpty(childControl.ID) && childControl.ID.Length > 3 && !dict.ContainsKey(childControl.ID.Substring(3)))
                    {
                        dict.Add(childControl.ID.Substring(3), childControl);
                    }
                }
            }
        }

        private static object GetControlValue(Control control, bool isComboxValue)
        {
            PropertyInfo propertyInfo;
            object value = null;
            switch (control.GetType().FullName)
            {
               
                case "System.Web.UI.WebControls.TextBox":
                    propertyInfo = control.GetType().GetProperty("Text");
                    value = propertyInfo.GetValue(control, null);
                    break;
              
                case "System.Web.UI.WebControls.CheckBox":
                    propertyInfo = control.GetType().GetProperty("Checked");
                    value = propertyInfo.GetValue(control, null);
                    break;
               
           
                case "System.Web.UI.WebControls.DropDownList":
                    propertyInfo = control.GetType().GetProperty("SelectedItem");
                    object item = propertyInfo.GetValue(control, null);
                    string text = "";
                    if (item != null)
                    {
                        if (!isComboxValue)
                        {
                            text = item.GetType().GetProperty("Text").GetValue(item, null) as string;
                        }
                        else
                        {
                            text = item.GetType().GetProperty("Value").GetValue(item, null) as string;
                        }
                    }
                    value = text;
                    break;
                default:
                    break;
            }
            return value;
        }
        private static void SetControlValue(Control control, object value)
        {
            PropertyInfo propertyInfo;
            switch (control.GetType().FullName)
            {
               
                case "System.Web.UI.WebControls.TextBox":
                    propertyInfo = control.GetType().GetProperty("Text");
                    propertyInfo.SetValue(control, Convert.ChangeType(value, propertyInfo.PropertyType), null);
                    break;
            
                case "System.Web.UI.WebControls.CheckBox":
                    propertyInfo = control.GetType().GetProperty("Checked");
                    propertyInfo.SetValue(control, Convert.ChangeType(value, propertyInfo.PropertyType), null);
                    break;
               
                case "System.Web.UI.WebControls.DropDownList":
                    string[] text = value.ToString().Split('=');
                    propertyInfo = control.GetType().GetProperty("Items");
                    object items = propertyInfo.GetValue(control, null);
                    if (!string.IsNullOrEmpty(text[0]))
                    {
                        MethodInfo methodInfo = items.GetType().GetMethod("FindByText");
                        propertyInfo = control.GetType().GetProperty("SelectedItem");
                        object item = methodInfo.Invoke(items, new object[] { text[0] });
                        if (item != null)
                        {
                            propertyInfo.SetValue(control, item, null);
                        }
                    }
                    else if (!string.IsNullOrEmpty(text[1]))
                    {
                        MethodInfo methodInfo = items.GetType().GetMethod("FindByValue");
                        propertyInfo = control.GetType().GetProperty("SelectedItem");
                        object item = methodInfo.Invoke(items, new object[] { text[1] });
                        if (item != null)
                        {
                            propertyInfo.SetValue(control, item, null);
                        }
                    }
                    break;
                default:
                    break;
            }
        }
    }
}
